package com.example.badyloans.Handler;

import com.example.badyloans.pojo.TUserAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author 啊桥哦
 * @Description:
 * @date 2020-07-13 15:29
 */
@Component
public class SessionHandler implements HandlerInterceptor {
    private  static  final Logger log= LoggerFactory.getLogger(SessionHandler.class);
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("当前请求路劲：{}",request.getRequestURI());
        //获取当前请求用户的sessionID
        String sessionId=request.getSession().getId();
//        String LoginUser = stringRedisTemplate.opsForValue().get(sessionId);
        TUserAccount tUserAccount= (TUserAccount) request.getSession().getAttribute("tUserAccount1");
        log.info("进入拦截器");
        if (StringUtils.isEmpty(tUserAccount)) {
            log.info("当前用户未登录，请求被拦截");
            //跳转登陆页面
            response.sendRedirect("/login.html");
            //证明当前用户未登录
            return false;
        }
        return true;
    }
}
